//
//  GPChatToolView.m
//  GuaPi
//
//  Created by 陈华 on 2017/6/19.
//  Copyright © 2017年 Joanlove. All rights reserved.
//

#import "GPChatToolView.h"

#define kMargin 20

@interface GPChatToolView () <UITextViewDelegate>

@end

@implementation GPChatToolView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
     
        //设置UI
        [self setupUI];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    //设置UI
    [self setupUI];
}


#pragma mark - 设置UI
/**设置UI*/
- (void)setupUI
{
    //设置属性
    self.backgroundColor = [UIColor whiteColor];
    
    //创建视图
    [self setupElement];
    
    //添加约束
    [self setupConstraint];
}

/**创建视图*/
- (void)setupElement
{
    //顶部分割线
    UIView *topSepartor = [[UIView alloc] init];
    topSepartor.backgroundColor = [UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1.0];
    [self addSubview:topSepartor];
    self.topSepartor = topSepartor;
    
    //表情按钮
    UIButton *emojiButton = [[UIButton alloc] init];
    [emojiButton setImage:[UIImage imageNamed:@"chat_face"] forState:UIControlStateNormal];
    [emojiButton addTarget:self action:@selector(emojiBtnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:emojiButton];
    self.emojiButton = emojiButton;
    
    //输入文本框
    UITextView *textView = [[UITextView alloc] init];
    ///--设置边框
    textView.layer.backgroundColor = [[UIColor clearColor] CGColor];
    textView.layer.borderColor = [[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0]CGColor];
    textView.layer.borderWidth = 1.0;
    textView.layer.cornerRadius = 8.0f;
    [textView.layer setMasksToBounds:YES];
    //--设置代理
    textView.delegate = self;
    //--设置其他
    textView.returnKeyType = UIReturnKeySend;
    textView.enablesReturnKeyAutomatically = YES;
    textView.autocorrectionType = UITextAutocorrectionTypeNo;
    textView.spellCheckingType = UITextSpellCheckingTypeNo;
    textView.showsVerticalScrollIndicator = NO;
    textView.showsHorizontalScrollIndicator = NO;
    textView.font = [UIFont systemFontOfSize:18.0];
    [self addSubview:textView];
    self.textView = textView;
    
    //发送按钮
    UIButton *sendButton = [[UIButton alloc] init];
    [sendButton addTarget:self action:@selector(sendBtnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    sendButton.backgroundColor = COLOR(76, 99, 243);
    [sendButton setTitle:@"Send" forState:UIControlStateNormal];
    [self addSubview:sendButton];
    self.sendButton = sendButton;
    
    //菜单栏
    GPChatToolMenuView *menuView = [GPChatToolMenuView toolMenuView];
    menuView.callback = ^(GPChatToolMenuViewType type, UIButton *button) {
      
        switch (type) {
            case GPChatToolMenuViewTypeEmoji:
            {
                if ([self.delegate respondsToSelector:@selector(toolView:didEmojiButtonClicked:)]) {
                    [self.delegate toolView:self didEmojiButtonClicked:button];
                }
            }
                break;
            case GPChatToolMenuViewTypeVoice:
            {
                if ([self.delegate respondsToSelector:@selector(toolView:didVoiceButtonClicked:)]) {
                    [self.delegate toolView:self didVoiceButtonClicked:button];
                }
            }
                break;
            case GPChatToolMenuViewTypePhotos:
            {
                if ([self.delegate respondsToSelector:@selector(toolView:didPhotosButtonClicked:)]) {
                    [self.delegate toolView:self didPhotosButtonClicked:button];
                }
            }
                break;
            case GPChatToolMenuViewTypeCamera:
            {
                if ([self.delegate respondsToSelector:@selector(toolView:didCarmeraButtonClicked:)]) {
                    [self.delegate toolView:self didCarmeraButtonClicked:button];
                }
            }
                break;
            case GPChatToolMenuViewTypeVideo:
            {
                if ([self.delegate respondsToSelector:@selector(toolView:didVideoButtonClicked:)]) {
                    [self.delegate toolView:self didVideoButtonClicked:button];
                }
            }
                break;
            case GPChatToolMenuViewTypeLocation:
            {
                if ([self.delegate respondsToSelector:@selector(toolView:didLocationButtonClicked:)]) {
                    [self.delegate toolView:self didLocationButtonClicked:button];
                }
            }
                break;
            default:
                break;
        }
        
    };
    [self addSubview:menuView];
    self.menuView = menuView;
}

/**创建约束*/
- (void)setupConstraint
{
    //顶部分割线
    [self.topSepartor mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(2);
    }];
    
    //表情按钮
    [self.emojiButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.mas_left).offset(kMargin * 0.5);
        make.left.mas_equalTo(self.mas_left);
        make.bottom.mas_equalTo(self.menuView.mas_top).offset(-7);
        make.width.height.mas_equalTo(0);
    }];
    
    //发送按钮
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-kMargin * 0.5);
        make.bottom.mas_equalTo(self.menuView.mas_top).offset(-7);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(40);
    }];
    
    //输入文本框
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.emojiButton.mas_right).offset(0.3 * kMargin);
        make.right.mas_equalTo(self.sendButton.mas_left).offset(-  0.3 * kMargin);
        make.bottom.mas_equalTo(self.menuView.mas_top).offset(-7);
        make.top.mas_equalTo(self).offset(7);
    }];
    
    //菜单栏
    [self.menuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(44);
    }];
    
}



#pragma mark - UITextViewDelegate
/**回车按钮是否可用*/
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //判断输入的字是否是回车，即按下return
    if ([text isEqualToString:@"\n"]){
        //在这里做你响应return键的代码
        if ([self.delegate respondsToSelector:@selector(toolView:didReturnKeyPressed:)]) {
            [self.delegate toolView:self didReturnKeyPressed:textView];
        }
        
        return NO;
    }
    
    
    if ([self.delegate respondsToSelector:@selector(toolView:editChanged:)]) {
        [self.delegate toolView:self editChanged:textView];
    }
    
    return YES;
}

/**是否允许开始编辑*/
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([self.delegate respondsToSelector:@selector(toolView:willBeginEditing:)]) {
        [self.delegate toolView:self willBeginEditing:textView];
    }
    return YES;
}

#pragma mark - 事件监听
/**表情按钮被点击*/
- (void)emojiBtnDidClicked:(UIButton *)emojieButton
{
    if ([self.delegate respondsToSelector:@selector(toolView:didEmojiButtonClicked:)]) {
        [self.delegate toolView:self didEmojiButtonClicked:emojieButton];
    }
}

/**发送按钮被点击*/
- (void)sendBtnDidClicked:(UIButton *)sendButton
{
    if ([self.delegate respondsToSelector:@selector(toolView:didSendButtonClicked:)]) {
        [self.delegate toolView:self didSendButtonClicked:sendButton];
    }
}

@end
